package nxu.it.controller;

import io.jooby.Context;
import io.jooby.MediaType;
import io.jooby.ModelAndView;
import io.jooby.annotation.*;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import nxu.it.model.ApiResult;
import nxu.it.service.GreetingService;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;

@Path("/")
@Singleton
public class MainController {
    GreetingService greetingService;

    @Inject
    public MainController(GreetingService greetingService) {
        this.greetingService = greetingService;
    }

    @GET(value = "/", produces = MediaType.HTML)
    public String index() {
        return "<h1>Yes, it works!</h1>";
    }

    @GET(value = "/hello", produces = MediaType.HTML)
    public String hello(@QueryParam String username, @QueryParam String gender) {
        return greetingService.sayHello(username,gender);
    }

    @GET(value = "/json", produces = MediaType.JSON)
    public ApiResult json() {
        return ApiResult.ok("请求成功");
    }

    @GET(value = "/render", produces = MediaType.HTML)
    public ModelAndView render(Context ctx) {
        return new ModelAndView("test.ftl", Map.of("name", "Jack"));
    }


    @POST(value = "/loginCheck", produces = MediaType.HTML)
    public String login(@FormParam("username") String username, @FormParam("password") String password) {
        if(username == null || password == null) {
            return "用户名或密码不能为空！";
        }
        if(username.length()<6 || password.length()<6) {
            return "用户名或密码长度不能小于6！";
        }
        return greetingService.login(username,password);
    }

    @GET(value = "/login", produces = MediaType.HTML)
    public ModelAndView loginFtl(Context ctx) {
        return new ModelAndView("login.ftl", Map.of("name", "Jack"));
    }


    @GET(value = "/register", produces = MediaType.HTML)
    public ModelAndView registerFtl(Context ctx) {
        List<String> list = Arrays.asList("宁夏","陕西","甘肃","其他");
        List<String> hobbies = Arrays.asList("唱歌","跳舞","篮球","说唱");
        Map<String, Object> model = new HashMap<>();
        model.put("hobbies", hobbies);
        model.put("provinces", list);
        return new ModelAndView("register.ftl", model);
    }

    @POST(value = "/doRegister", produces = MediaType.HTML)
    public String doRegister(
            @FormParam("username") String username,
            @FormParam("password") String password,
            @FormParam("gender") String gender,
            @FormParam("age") int age,
            @FormParam("province") String province,
            @FormParam("email") String email,
            @FormParam("hobbies") String hobbies) {

        String str = "我的用户名是"+username+",密码是"+password+",性别是"+gender+",今年"+age+"岁,籍贯是"+province+",邮箱是"+email+",爱好是"+hobbies+"。";
        return str;
    }

    @GET(value = "/today", produces = MediaType.JSON)
    public Map<String, String> getToday() {
        Map<String, String> dateInfo = new HashMap<>();
        Date now = new Date();
        dateInfo.put("date", now.toString());
        dateInfo.put("time", String.valueOf(now.getTime()));
        return dateInfo;
    }

}
